<?php

namespace App\Admin\Controllers;

use App\Models\Banner;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Support\Facades\Auth;

class BannerController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = 'Banner';

    private function __($key)
    {
        return [
            'Id' => 'ID',
            'Admin user' => '管理员',
            'Type' => '类型',
            'Title' => '标题',
            'Img' => '图片',
            'Sort' => '排序',
            'Status' => '状态',
            'Created at' => '创建时间',
            'Updated at' => '修改时间',
        ][$key] ?? $key;
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Banner());
        // $grid->model()->where('type', 0);

        $grid->disableFilter();
        $grid->disableExport();
        $grid->disableColumnSelector();

        $grid->column('id', __('Id'))->filter();
        $grid->column('img', __('Img'))->image('/storage', 50, 50);
        $grid->column('adminUser.name', __('Admin user'))->filter();
        // $grid->column('type', __('Type'));
        $grid->column('title', __('Title'))->filter('like');
        $grid->column('sort', __('Sort'))->sortable();
        $grid->column('status', __('Status'))->icon([
            0 => 'toggle-off',
            1 => 'toggle-on',
        ])->filter([
            0 => '禁用',
            1 => '启用',
        ]);
        $grid->column('created_at', __('Created at'))->display(function($created) {
            return date('Y-m-d H:i:s', strtotime($created));
        })->sortable();
        // $grid->column('updated_at', __('Updated at'));
        // $grid->column('data', __('Data'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Banner::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('adminUser.name', __('Admin user'));
        // $show->field('type', __('Type'));
        $show->field('title', __('Title'));
        $show->field('img', __('Img'))->image('/storage', 100, 100);
        $show->field('sort', __('Sort'));
        $show->field('status', __('Status'))->using([
            0 => '禁用',
            1 => '启用',
        ]);
        $show->field('created_at', __('Created at'))->display(function($created) {
            return date('Y-m-d H:i:s', strtotime($created));
        });
        $show->field('updated_at', __('Updated at'))->display(function($created) {
            return date('Y-m-d H:i:s', strtotime($created));
        });
        // $show->field('data', __('Data'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Banner());
       
        $form->hidden('admin_user_id')->default(0);
        // $form->select('type', __('Type'));
        $form->text('title', __('Title'));
        $form->image('img', __('Img'));
        if ($form->isCreating()) {
            $maxSort = Banner::max('sort');
            $form->number('sort', __('Sort'))->default($maxSort ? ($maxSort + 1) : 1);
        } else {
            $form->number('sort', __('Sort'));
        }
        $form->switch('status', __('Status'));
        // $form->text('data', __('Data'));

        return $form;
    }
}
